//
//  TTMineHeaderView.m
//  TaoTuo
//
//  Created by admin on 2019/10/17.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTMineHeaderView.h"
#import <UIImageView+WebCache.h>
#import "TTLoginViewController.h"
#import "TTUserInfoViewController.h"
#import "TTInvitationCodeViewController.h"

@interface TTMineHeaderView ()

@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIImageView *sfBGImageView;
@property (nonatomic, strong) UILabel *sfLabel;
@property (nonatomic, strong) UIButton *loginBtn;
///邀请好友按钮
@property (nonatomic, strong) UIButton *invitationBtn;

@end

@implementation TTMineHeaderView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupSubviews];
        
    }
    return self;
}

// 登录
- (void)loginAction
{
    UIViewController *vc = [LLRouteTool getRootViewController];
    [vc presentLoginViewController];
}

/// 邀请二维码
- (void)lookInvitationCode
{
    TTInvitationCodeViewController *invitationVC = [[TTInvitationCodeViewController alloc] init];
    [[LLRouteTool getCurrentNavigationController] pushViewController:invitationVC animated:YES];
}

// 设置用户数据
- (void)setUserData
{
    TTLoginSingle *single = [TTLoginSingle sharedSingle];
    if (single.isLogin) {
        _loginBtn.hidden = YES;
        _nameLabel.hidden = NO;
        _sfLabel.hidden = NO;
    } else {
        _loginBtn.hidden = NO;
        _nameLabel.hidden = YES;
        _sfLabel.hidden = YES;
    }
    
    _nameLabel.text = single.loginModel.RealName;
    if (single.userModel.VipLevel == 1) {
        _sfBGImageView.image = [[UIImage imageNamed:@"shangjia_bg"] resizedImage];
        _sfLabel.text = [NSString stringWithFormat:@"商家"];
        _sfLabel.textColor = UIColorHex(814F24);
        _sfLabel.layer.backgroundColor = [UIColor clearColor].CGColor;
    } else {
        _sfBGImageView.image = [UIImage new];
        _sfLabel.text = [NSString stringWithFormat:@"普通会员"];
        _sfLabel.textColor = [UIColor whiteColor];
        _sfLabel.layer.backgroundColor = UIColorHex(282828).CGColor;
    }
    
    [_headerImageView sd_setImageWithURL:[NSURL URLWithString:single.loginModel.Picture] placeholderImage:[UIImage imageNamed:@"header_img"] options:SDWebImageRetryFailed];
}

///点击头像
- (void)tapHeaderAction
{
    TTUserInfoViewController *userVC = [[TTUserInfoViewController alloc] init];
    [[LLRouteTool getCurrentNavigationController] pushViewController:userVC animated:YES];
}

- (void)setupSubviews
{
    // 头像
    _headerImageView = [[UIImageView alloc] init];
    _headerImageView.contentMode = UIViewContentModeScaleAspectFill;
    _headerImageView.layer.cornerRadius = 30;
    _headerImageView.layer.masksToBounds = YES;
    _headerImageView.userInteractionEnabled = YES;
    [self addSubview:_headerImageView];
    [_headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).mas_offset(-18);
        make.left.mas_equalTo(30);
        make.width.height.mas_equalTo(60);
    }];
    //点击头像
    UITapGestureRecognizer *tapHeader = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHeaderAction)];
    [_headerImageView addGestureRecognizer:tapHeader];
    
    // 姓名
    _nameLabel = [UILabel labelTextCenterWithTitle:@"- -" color:[UIColor whiteColor] fontSize:16];
    [self addSubview:_nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerImageView);
        make.left.equalTo(self.headerImageView.mas_right).mas_offset(10);
        make.bottom.equalTo(self.headerImageView.mas_centerY);
    }];
    
    // 身份
    _sfBGImageView = [[UIImageView alloc] init];
    _sfBGImageView.userInteractionEnabled = YES;
    [self addSubview:_sfBGImageView];
    [_sfBGImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom);
        make.left.equalTo(self.nameLabel);
        make.height.mas_equalTo(20);
        make.width.mas_greaterThanOrEqualTo(60);
    }];
    
    @weakify(self)
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        @strongify(self)
        if (self.clickSFZBlock) {
            self.clickSFZBlock();
        }
    }];
    [_sfBGImageView addGestureRecognizer:tap];
    
    _sfLabel = [UILabel labelTextCenterWithTitle:@"商家" color:[UIColor whiteColor] fontSize:11];
    _sfLabel.layer.cornerRadius = 10;
    [self addSubview:_sfLabel];
    [_sfLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.sfBGImageView);
    }];
    
    _loginBtn = [UIButton createButtonWithTitle:@"登录" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:16] backgroundColor:nil target:self action:@selector(loginAction)];
    _loginBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [self addSubview:_loginBtn];
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(60 + 45);
        make.top.left.height.equalTo(self.headerImageView);
    }];
    
    _invitationBtn = [UIButton createButtonWithTitle:@"邀请好友" titleColor:UIColorHex(7F4DD6) titleFont:[UIFont systemFontOfSize:12] backgroundColor:[UIColor whiteColor] cornerRadius:12 target:self action:@selector(lookInvitationCode)];
    [_invitationBtn setImage:[UIImage imageNamed:@"miniyaoqinghaoyou"] forState:UIControlStateNormal];
    _invitationBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 8);
    _invitationBtn.isNeedLogin = YES;
    [self addSubview:_invitationBtn];
    [_invitationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).mas_offset(-10);
        make.centerY.equalTo(self.headerImageView);
        make.height.mas_equalTo(24);
    }];
}


@end
